<?php
/**
 * Created by PhpStorm.
 * User: yafex
 * Date: 2020/8/2
 * Time: 16:27
 */
class serv_rapi_wish extends L {

    public static $url = "https://merchant.wish.com/api/";
    /**
     * 描述 : 用code获取token
     * 作者 : Zoro.Zhu
     */
    public static function codeToken($code,$clientId,$secret){
        $url = self::$url . "v3/oauth/access_token";
        $json = [
            'client_id' => $clientId,
            'client_secret' => $secret,
            'code' => $code,
            'grant_type' => 'authorization_code',
            'redirect_uri' => 'https://6beams.com/serv/accreditwish.php',
        ];
        $data = http_build_query($json);
        $res = of_base_com_net::request($url . '?' . $data, ['type' => 'get']);
        if(!empty($res['state'])){
            return json_decode($res['response'],true);
        }else{
            return [];
        }
    }

    /**
     * 描述 : 刷新token
     * 作者 : Zoro.Zhu
     **/
    public static function refreshToken($data) {
        $url = self::$url . "v3/oauth/refresh_token";
        $json = [
            'client_id' => $data['appKey'],
            'client_secret' => $data['appSecret'],
            'refresh_token' => $data['refreshToken'],
            'grant_type' => 'refresh_token',
        ];
        $data = http_build_query($json);
        $res = of_base_com_net::request($url . '?' . $data, ['type' => 'get']);
        if(!empty($res['state'])){
            return json_decode($res['response'],true);
        }else if(empty($data['retry']) || $data['retry'] < 3){
            //重试3次
            $data['retry'] = !empty($data['retry']) ? $data['retry'] + 1 : 1;
            self::refreshToken($data);
        }else{
            return [];
        }
    }

    /**
     * 描述 : 获取送货设置
     * 作者 : Zoro.Zhu
     */
    public static function getShipping($data){
        $url = self::$url . "v3/merchant/shipping_settings";
        $res = of_base_com_net::request($url, ['type' => 'get', 'header' => ['Authorization: Bearer ' . $data['accessToken']]]);
        if(!empty($res['state'])){
            return json_decode($res['response'],true);
        }else{
            return [];
        }
    }

    /**
     * 描述 : 设置配送国家
     * 作者 : Zoro.Zhu
     */
    public static function setShipping($data, $country){
        $url = self::$url . "v3/merchant/shipping_settings";
        $res = of_base_com_net::request($url, ['type' => 'put', 'data' => json_encode($country), 'header' => ['Authorization: Bearer ' . $data['accessToken']]]);
        if(!empty($res['state'])){
            return json_decode($res['response'],true);
        }else{
            return [];
        }
    }

    /**
     * 描述 : 获取产品列表
     * 作者 : Zoro.Zhu
     * 注明 : 接口响应很慢
     */
    public static function getProduct($data){
        $url = "https://merchant.wish.com/api/v2/product/multi-get?limit={$data['limit']}&start={$data['start']}&since={$data['since']}&access_token={$data['accessToken']}";
        $res = of_base_com_net::request($url,['type' => 'get']);
        if($res['state']){
            return json_decode($res['response'],true);
        } else {
            return [];
        }
    }

    /**
     * 描述 : 获取产品信息
     * 作者 : Zoro.Zhu
     */
    public static function getProductData($data){
        $url = "https://merchant.wish.com/api/v2/product?id={$data['id']}&access_token={$data['accessToken']}";
        $res = of_base_com_net::request($url,['type' => 'get']);
        if($res['state']){
            return json_decode($res['response'],true);
        } else {
            return [];
        }
    }

    /**
     * 描述 : 修改运费
     * 作者 : Zoro.Zhu
     */
    public static function updateShipping($data){
        $url = "https://merchant.wish.com/api/v2/product/update-multi-shipping";
        $json = [
            'id' => $data['id'],
            'access_token' => $data['accessToken'],
            'localized_currency_code' => 'CNY',
        ];
        $json += $data['fee'];
        $data = http_build_query($json);
        $res = of_base_com_net::request($url . '?' . $data, ['type' => 'post']);
        if($res['state']) {
            $response = json_decode($res['response'],true);
            if($response['code'] === 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 描述 : 修改SKU信息
     * 作者 : Zoro.Zhu
     */
    public static function updateSkuInfo($data){
        $url = "https://merchant.wish.com/api/v2/variant/update";
        $data = http_build_query($data);
        $res = of_base_com_net::request($url . '?' . urlencode($data), ['type' => 'post']);
        if($res['state']) {
            $response = json_decode($res['response'],true);
            if($response['code'] === 0) {
                return true;
            }
        }
        if(!empty($res['errstr'])){
            $errstr = json_decode($res['errstr'],true);
            if(!empty($errstr['message'])){
                return $errstr['message'];
            }
        }
        return false;
    }

    /**
     * 描述 : 获取客服问题信息列表
     * 作者 : Zoro.Zhu
     */
    public static function getTicket($data) {
        $url = "https://merchant.wish.com/api/v2/ticket/get-action-required";
        $json = [
            'access_token' => $data['accessToken'],
        ];
        $data = http_build_query($json);
        $res = of_base_com_net::request($url . '?' . $data, ['type' => 'get']);
        if($res['state']) {
            $response = json_decode($res['response'],true);
            if($response['code'] === 0) {
                return $response['data'];
            }
        }
        return false;
    }

    /**
     * 描述 : 修改产品信息
     * 作者 : Zoro.Zhu
     */
    public static function productUpdate($data){
        $url = "https://merchant.wish.com/api/v2/product/update";
        $data = http_build_query($data);
        $res = of_base_com_net::request($url . '?' . $data, ['type' => 'post']);
        if($res['state']) {
            $response = json_decode($res['response'],true);
            if($response['code'] === 0) {
                return true;
            }
        }
        if(!empty($res['errstr'])){
            $errstr = json_decode($res['errstr'],true);
            if(!empty($errstr['message'])){
                return $errstr['message'];
            }
        }
        return false;
    }


    /**
     * 描述 : 获取PB广告数据
     * 作者 : Zoro.Zhu
     */
    public static function getPbList($data){
        $url = self::$url . "v3/product_boost/campaigns";
        $res = of_base_com_net::request($url, ['type' => 'get', 'header' => ['Authorization: Bearer ' . $data['accessToken']]]);
        if(!empty($res['state'])){
            return json_decode($res['response'],true);
        }else{
            return [];
        }
    }

}
return true;